package com.koala.aideddevsystemmodule.modules.idempotent.service.impl;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.koala.aideddevsystemmodule.modules.idempotent.dao.IdempotentDao;
import com.koala.aideddevsystemmodule.modules.idempotent.entity.IdempotentEntity;
import com.koala.aideddevsystemmodule.modules.idempotent.service.IdempotentService;

import javax.annotation.Resource;

/**
 * Create by koala on 2022-08-21 16:35:28
 */
@Service("idempotentService")
public class IdempotentServiceImpl extends ServiceImpl<IdempotentDao, IdempotentEntity> implements IdempotentService {

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public Boolean idempotentSaveByRedis(IdempotentEntity idempotent) {
        String key = idempotent.getId();
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(key, idempotent, 1L, TimeUnit.DAYS);

        if(aBoolean){
            //TODO 幂等校验通过，执行后续业务
        }

        return aBoolean;
    }

    @Override
    public Boolean idempotentSaveBySqlTable(IdempotentEntity idempotent) {
        boolean save = false;
        try {
            save = this.save(idempotent);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(save){
            //TODO 幂等校验通过，执行后续业务
        }

        return save;
    }

}